{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一、引入数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['data', 'target', 'target_names', 'DESCR', 'feature_names'])"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.datasets import load_breast_cancer\n",
    "cancer=load_breast_cancer()\n",
    "cancer.keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、将数据集转换为DataFram:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mean radius</th>\n",
       "      <th>mean texture</th>\n",
       "      <th>mean perimeter</th>\n",
       "      <th>mean area</th>\n",
       "      <th>mean smoothness</th>\n",
       "      <th>mean compactness</th>\n",
       "      <th>mean concavity</th>\n",
       "      <th>mean concave points</th>\n",
       "      <th>mean symmetry</th>\n",
       "      <th>mean fractal dimension</th>\n",
       "      <th>...</th>\n",
       "      <th>worst texture</th>\n",
       "      <th>worst perimeter</th>\n",
       "      <th>worst area</th>\n",
       "      <th>worst smoothness</th>\n",
       "      <th>worst compactness</th>\n",
       "      <th>worst concavity</th>\n",
       "      <th>worst concave points</th>\n",
       "      <th>worst symmetry</th>\n",
       "      <th>worst fractal dimension</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>17.99</td>\n",
       "      <td>10.38</td>\n",
       "      <td>122.80</td>\n",
       "      <td>1001.0</td>\n",
       "      <td>0.11840</td>\n",
       "      <td>0.27760</td>\n",
       "      <td>0.30010</td>\n",
       "      <td>0.14710</td>\n",
       "      <td>0.2419</td>\n",
       "      <td>0.07871</td>\n",
       "      <td>...</td>\n",
       "      <td>17.33</td>\n",
       "      <td>184.60</td>\n",
       "      <td>2019.0</td>\n",
       "      <td>0.16220</td>\n",
       "      <td>0.66560</td>\n",
       "      <td>0.7119</td>\n",
       "      <td>0.2654</td>\n",
       "      <td>0.4601</td>\n",
       "      <td>0.11890</td>\n",
       "      <td>malignant</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20.57</td>\n",
       "      <td>17.77</td>\n",
       "      <td>132.90</td>\n",
       "      <td>1326.0</td>\n",
       "      <td>0.08474</td>\n",
       "      <td>0.07864</td>\n",
       "      <td>0.08690</td>\n",
       "      <td>0.07017</td>\n",
       "      <td>0.1812</td>\n",
       "      <td>0.05667</td>\n",
       "      <td>...</td>\n",
       "      <td>23.41</td>\n",
       "      <td>158.80</td>\n",
       "      <td>1956.0</td>\n",
       "      <td>0.12380</td>\n",
       "      <td>0.18660</td>\n",
       "      <td>0.2416</td>\n",
       "      <td>0.1860</td>\n",
       "      <td>0.2750</td>\n",
       "      <td>0.08902</td>\n",
       "      <td>malignant</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>19.69</td>\n",
       "      <td>21.25</td>\n",
       "      <td>130.00</td>\n",
       "      <td>1203.0</td>\n",
       "      <td>0.10960</td>\n",
       "      <td>0.15990</td>\n",
       "      <td>0.19740</td>\n",
       "      <td>0.12790</td>\n",
       "      <td>0.2069</td>\n",
       "      <td>0.05999</td>\n",
       "      <td>...</td>\n",
       "      <td>25.53</td>\n",
       "      <td>152.50</td>\n",
       "      <td>1709.0</td>\n",
       "      <td>0.14440</td>\n",
       "      <td>0.42450</td>\n",
       "      <td>0.4504</td>\n",
       "      <td>0.2430</td>\n",
       "      <td>0.3613</td>\n",
       "      <td>0.08758</td>\n",
       "      <td>malignant</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>566</th>\n",
       "      <td>16.60</td>\n",
       "      <td>28.08</td>\n",
       "      <td>108.30</td>\n",
       "      <td>858.1</td>\n",
       "      <td>0.08455</td>\n",
       "      <td>0.10230</td>\n",
       "      <td>0.09251</td>\n",
       "      <td>0.05302</td>\n",
       "      <td>0.1590</td>\n",
       "      <td>0.05648</td>\n",
       "      <td>...</td>\n",
       "      <td>34.12</td>\n",
       "      <td>126.70</td>\n",
       "      <td>1124.0</td>\n",
       "      <td>0.11390</td>\n",
       "      <td>0.30940</td>\n",
       "      <td>0.3403</td>\n",
       "      <td>0.1418</td>\n",
       "      <td>0.2218</td>\n",
       "      <td>0.07820</td>\n",
       "      <td>malignant</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>567</th>\n",
       "      <td>20.60</td>\n",
       "      <td>29.33</td>\n",
       "      <td>140.10</td>\n",
       "      <td>1265.0</td>\n",
       "      <td>0.11780</td>\n",
       "      <td>0.27700</td>\n",
       "      <td>0.35140</td>\n",
       "      <td>0.15200</td>\n",
       "      <td>0.2397</td>\n",
       "      <td>0.07016</td>\n",
       "      <td>...</td>\n",
       "      <td>39.42</td>\n",
       "      <td>184.60</td>\n",
       "      <td>1821.0</td>\n",
       "      <td>0.16500</td>\n",
       "      <td>0.86810</td>\n",
       "      <td>0.9387</td>\n",
       "      <td>0.2650</td>\n",
       "      <td>0.4087</td>\n",
       "      <td>0.12400</td>\n",
       "      <td>malignant</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>568</th>\n",
       "      <td>7.76</td>\n",
       "      <td>24.54</td>\n",
       "      <td>47.92</td>\n",
       "      <td>181.0</td>\n",
       "      <td>0.05263</td>\n",
       "      <td>0.04362</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.1587</td>\n",
       "      <td>0.05884</td>\n",
       "      <td>...</td>\n",
       "      <td>30.37</td>\n",
       "      <td>59.16</td>\n",
       "      <td>268.6</td>\n",
       "      <td>0.08996</td>\n",
       "      <td>0.06444</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.2871</td>\n",
       "      <td>0.07039</td>\n",
       "      <td>benign</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6 rows × 31 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     mean radius  mean texture  mean perimeter  mean area  mean smoothness  \\\n",
       "0          17.99         10.38          122.80     1001.0          0.11840   \n",
       "1          20.57         17.77          132.90     1326.0          0.08474   \n",
       "2          19.69         21.25          130.00     1203.0          0.10960   \n",
       "566        16.60         28.08          108.30      858.1          0.08455   \n",
       "567        20.60         29.33          140.10     1265.0          0.11780   \n",
       "568         7.76         24.54           47.92      181.0          0.05263   \n",
       "\n",
       "     mean compactness  mean concavity  mean concave points  mean symmetry  \\\n",
       "0             0.27760         0.30010              0.14710         0.2419   \n",
       "1             0.07864         0.08690              0.07017         0.1812   \n",
       "2             0.15990         0.19740              0.12790         0.2069   \n",
       "566           0.10230         0.09251              0.05302         0.1590   \n",
       "567           0.27700         0.35140              0.15200         0.2397   \n",
       "568           0.04362         0.00000              0.00000         0.1587   \n",
       "\n",
       "     mean fractal dimension    ...      worst texture  worst perimeter  \\\n",
       "0                   0.07871    ...              17.33           184.60   \n",
       "1                   0.05667    ...              23.41           158.80   \n",
       "2                   0.05999    ...              25.53           152.50   \n",
       "566                 0.05648    ...              34.12           126.70   \n",
       "567                 0.07016    ...              39.42           184.60   \n",
       "568                 0.05884    ...              30.37            59.16   \n",
       "\n",
       "     worst area  worst smoothness  worst compactness  worst concavity  \\\n",
       "0        2019.0           0.16220            0.66560           0.7119   \n",
       "1        1956.0           0.12380            0.18660           0.2416   \n",
       "2        1709.0           0.14440            0.42450           0.4504   \n",
       "566      1124.0           0.11390            0.30940           0.3403   \n",
       "567      1821.0           0.16500            0.86810           0.9387   \n",
       "568       268.6           0.08996            0.06444           0.0000   \n",
       "\n",
       "     worst concave points  worst symmetry  worst fractal dimension     target  \n",
       "0                  0.2654          0.4601                  0.11890  malignant  \n",
       "1                  0.1860          0.2750                  0.08902  malignant  \n",
       "2                  0.2430          0.3613                  0.08758  malignant  \n",
       "566                0.1418          0.2218                  0.07820  malignant  \n",
       "567                0.2650          0.4087                  0.12400  malignant  \n",
       "568                0.0000          0.2871                  0.07039     benign  \n",
       "\n",
       "[6 rows x 31 columns]"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "cancer_data=pd.DataFrame(cancer.data,columns=cancer.feature_names)\n",
    "cancer_data['target']=cancer.target_names[cancer.target]\n",
    "cancer_data.head(3).append(cancer_data.tail(3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、可视化数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.PairGrid at 0x24968353048>"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "IOPub data rate exceeded.\n",
      "The notebook server will temporarily stop sending output\n",
      "to the client in order to avoid crashing it.\n",
      "To change this limit, set the config variable\n",
      "`--NotebookApp.iopub_data_rate_limit`.\n"
     ]
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import seaborn as sns\n",
    "sns.pairplot(cancer_data,hue='target',palette='husl')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二、数据预处理之数据标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "X=StandardScaler().fit_transform(cancer.data)\n",
    "y=cancer.target"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 三、数据集划分为训练集和测试集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.25,random_state=33) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 四、创建模型估计器estimator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 五、模型拟合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 六、模型性能评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 逻辑回归用于分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集的分类准确率为: 0.986013986014\n",
      "             precision    recall  f1-score   support\n",
      "\n",
      "  malignant       1.00      0.96      0.98        54\n",
      "     benign       0.98      1.00      0.99        89\n",
      "\n",
      "avg / total       0.99      0.99      0.99       143\n",
      "\n",
      "Accuracy: 0.986013986014\n",
      "best params: {'C': 0.1, 'max_iter': 100, 'solver': 'liblinear'}\n"
     ]
    }
   ],
   "source": [
    "#创建模型估计器estimator\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "lgr=LogisticRegression()\n",
    "\n",
    "#用训练集训练模型估计器estimator\n",
    "lgr.fit(X_train,y_train)\n",
    "\n",
    "#用模型估计器对测试集数据做预测\n",
    "y_pred=lgr.predict(X_test)\n",
    "\n",
    "#对模型估计器的学习效果进行评价\n",
    "#最简单的评估方法：就是调用估计器的score(),该方法的两个参数要求是测试集的特征矩阵和标签向量\n",
    "print(\"测试集的分类准确率为:\",lgr.score(X_test,y_test))\n",
    "from sklearn import metrics\n",
    "#对于多分类问题，还可以使用metrics子包中的classification_report\n",
    "print(metrics.classification_report(y_test,y_pred,target_names=cancer.target_names)) \n",
    "\n",
    "#网格搜索与交叉验证相结合的逻辑回归算法分类：\n",
    "from sklearn.model_selection import GridSearchCV,KFold\n",
    "params_lgr={'C':[0.01,0.1,1,10,100],'max_iter':[100,200,300],'solver':['liblinear','lbfgs']}\n",
    "kf=KFold(n_splits=5,shuffle=False)\n",
    "\n",
    "grid_search_lgr=GridSearchCV(lgr,params_lgr,cv=kf)\n",
    "grid_search_lgr.fit(X_train,y_train)\n",
    "grid_search_y_pred=grid_search_lgr.predict(X_test)\n",
    "print(\"Accuracy:\",grid_search_lgr.score(X_test,y_test))\n",
    "print(\"best params:\",grid_search_lgr.best_params_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 支持向量用于分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集的分类准确率为: 0.986013986014\n",
      "             precision    recall  f1-score   support\n",
      "\n",
      "  malignant       1.00      0.96      0.98        54\n",
      "     benign       0.98      1.00      0.99        89\n",
      "\n",
      "avg / total       0.99      0.99      0.99       143\n",
      "\n",
      "Accuracy: 0.986013986014\n",
      "best params: {'C': 0.1, 'gamma': 0.1, 'kernel': 'linear'}\n"
     ]
    }
   ],
   "source": [
    "#创建模型估计器estimator\n",
    "from sklearn.svm import SVC\n",
    "svc=SVC()\n",
    "\n",
    "#用训练集训练模型估计器estimator\n",
    "svc.fit(X_train,y_train)\n",
    "\n",
    "#用模型估计器对测试集数据做预测\n",
    "y_pred=svc.predict(X_test)\n",
    "\n",
    "#对模型估计器的学习效果进行评价\n",
    "#最简单的评估方法：就是调用估计器的score(),该方法的两个参数要求是测试集的特征矩阵和标签向量\n",
    "print(\"测试集的分类准确率为:\",svc.score(X_test,y_test))\n",
    "from sklearn import metrics\n",
    "#对于多分类问题，还可以使用metrics子包中的classification_report\n",
    "print(metrics.classification_report(y_test,y_pred,target_names=cancer.target_names))\n",
    "\n",
    "#网格搜索与交叉验证相结合的SVM算法分类：\n",
    "from sklearn.model_selection import GridSearchCV,KFold\n",
    "params_svc={'C':[0.1,1,10],'gamma':[0.1,1,10],'kernel':['linear','rbf']}\n",
    "kf=KFold(n_splits=5,shuffle=False)\n",
    "grid_search_svc=GridSearchCV(svc,params_svc,cv=kf)\n",
    "grid_search_svc.fit(X_train,y_train)\n",
    "grid_search_y_pred=grid_search_svc.predict(X_test)\n",
    "print(\"Accuracy:\",grid_search_svc.score(X_test,y_test))\n",
    "print(\"best params:\",grid_search_svc.best_params_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## kNN用于分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集的分类准确率为: 0.965034965035\n",
      "             precision    recall  f1-score   support\n",
      "\n",
      "  malignant       0.98      0.93      0.95        54\n",
      "     benign       0.96      0.99      0.97        89\n",
      "\n",
      "avg / total       0.97      0.97      0.96       143\n",
      "\n",
      "Accuracy: 0.965034965035\n",
      "best params: {'algorithm': 'auto', 'n_neighbors': 5, 'weights': 'uniform'}\n"
     ]
    }
   ],
   "source": [
    "#创建模型估计器estimator\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "knn=KNeighborsClassifier()\n",
    "\n",
    "#用训练集训练模型估计器estimator\n",
    "knn.fit(X_train,y_train)\n",
    "\n",
    "#用模型估计器对测试集数据做预测\n",
    "y_pred=knn.predict(X_test)\n",
    "\n",
    "#对模型估计器的学习效果进行评价\n",
    "#最简单的评估方法：就是调用估计器的score(),该方法的两个参数要求是测试集的特征矩阵和标签向量\n",
    "print(\"测试集的分类准确率为:\",knn.score(X_test,y_test))\n",
    "from sklearn import metrics\n",
    "#对于多分类问题，还可以使用metrics子包中的classification_report\n",
    "print(metrics.classification_report(y_test,y_pred,target_names=cancer.target_names))\n",
    "\n",
    "#网格搜索与交叉验证相结合的kNN算法分类：\n",
    "from sklearn.model_selection import GridSearchCV,KFold\n",
    "params_knn={'algorithm':['auto','ball_tree','kd_tree','brute'],'n_neighbors':range(3,10,1),'weights':['uniform','distance']}\n",
    "kf=KFold(n_splits=5,shuffle=False)\n",
    "grid_search_knn=GridSearchCV(knn,params_knn,cv=kf)\n",
    "grid_search_knn.fit(X_train,y_train)\n",
    "grid_search_y_pred=grid_search_knn.predict(X_test)\n",
    "print(\"Accuracy:\",grid_search_knn.score(X_test,y_test))\n",
    "print(\"best params:\",grid_search_knn.best_params_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
